<?php
include("header.php");
echo "<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />";
if($status_connect){
		/*------------ Owner ------------*/
		 $Initial = $_REQUEST['Initial'];
		 $txtNameOwner = $_REQUEST['txtNameOwner'];
		 $NumHome = $_REQUEST['txtNumHome'];
		 $txtNumVillage = $_REQUEST['txtNumVillage'];
		 $Name = $_REQUEST['district_1'];
		 $Tel = $_REQUEST['Tel'];
		 $Phone = $_REQUEST['Phone'];
		 $Fax = $_REQUEST['Fax'];
		 $datepicker = $_REQUEST['datepicker'];
		 $Recipient = $_REQUEST['Recipient'];
		 /*----------- Farm ------------*/
		 $txtReg = $_REQUEST['txtReg'];
		 $txtNameFarm = $_REQUEST['txtNameFarm'];
		 $txtNumFarm = $_REQUEST['txtNumFarm'];
		 $txtNumVillageFarm = $_REQUEST['txtNumVillageFarm'];
		 $district_2 = $_REQUEST['district_2'];
		 $txtTelFarm = $_REQUEST['txtTelFarm'];
		 $txtPhoneFarm = $_REQUEST['txtPhoneFarm'];
		 $txtFaxFarm = $_REQUEST['txtFaxFarm'];
		 $typefarm = $_REQUEST['typefarm'];
		 $txtTypeFarm = $_REQUEST['type_farm'];
		 $FarmISO = $_REQUEST['FarmISO'];
		 $MainWay = $_REQUEST['MainWay'];
		 $MarketAnimal = $_REQUEST['MarketAnimal'];
		 $Abbattoir = $_REQUEST['Abbattoir'];
		 $Village = $_REQUEST['Village'];
		 $PublicWater = $_REQUEST['PublicWater'];
		 $OtherFarm = $_REQUEST['OtherFarm'];
		 $Area_ha = $_REQUEST['Area_ha'];
		 $Area_work = $_REQUEST['Area_work'];
		 $Area_table = $_REQUEST['Area_table'];
		 $Fence = $_REQUEST['FenceDetail'];
		 $txtNumOffice = $_REQUEST['txtNumOffice'];
		 $txtNumHome = $_REQUEST['txtNumHome'];
		 $txtNumHouse = $_REQUEST['txtNumHouse'];
		 $sizeHouse = "";
		 if($_REQUEST['txtSizeHouse1'] != NULL){ $sizeHouse .= $_REQUEST['txtSizeHouse1']."-"; }
		 if($_REQUEST['txtSizeHouse2'] != NULL){ $sizeHouse .= $_REQUEST['txtSizeHouse2']."-"; }
		 if($_REQUEST['txtSizeHouse3'] != NULL){ $sizeHouse .= $_REQUEST['txtSizeHouse3'];}
		 else{ $sizeHouse = substr($sizeHouse,0,strlen($sizeHouse)-1);}
		 $House = $_REQUEST['House'];
		 $Roof = $_REQUEST['Roof'];
		 $Ground = $_REQUEST['Ground'];
		 $SizeStableImport = "";
		 $NumStableImport = "";
		if($_REQUEST['StableImport'] == "1"){
		 $SizeStableImport = $_REQUEST['SizeStableImport'];
		 $NumStableImport = $_REQUEST['NumStableImport'];
		}
		$SizeStableDisease = "";
		$NumStableDisease = "";
		 if($_REQUEST['StableDisease'] == "1"){
		 $SizeStableDisease = $_REQUEST['SizeStableDisease'];
		 $NumStableDisease = $_REQUEST['NumStableDisease'];
		 }
		 $Ascent = $_REQUEST['Material'];
		 $PackAnimals = $_REQUEST['NumPackAnimals'];
		 $Water = $_REQUEST['Water']; 
		 if($_REQUEST['OtherWater'] != NULL){ $Water .= "".$_REQUEST['OtherWater'];}
		 $NumWorker = $_REQUEST['NumWorker'];
		 $HomeWorker = $_REQUEST['HomeWorker'];
		 $WaterTreatment = $_REQUEST['WaterTreatment'];
		 $SpraySolution = $_REQUEST['SpraySolution'];
		 $Record = $_REQUEST['Record'];
		 $Month6 = $_REQUEST['Month6'];
		 $Year1 = $_REQUEST['Year1'];
		 $Year3 = $_REQUEST['Year3'];
		 $Year5 = $_REQUEST['Year5'];
		 $Cesspool = $_REQUEST['Cesspool'];
		 if($_REQUEST_['Wastewater'] != NULL){
			 $NumPothole = $_REQUEST['NumPothole'];
			 $WidthPothole = $_REQUEST['WidthPothole'];
			 $HeightPothole = $_REQUEST['HeightPothole'];
			 $DepthPothole = $_REQUEST['DepthPothole'];
		 }
		 $DrainageWaste = $_REQUEST['DrainageWaste'];
		 if($_REQUEST['DebrisRemoval'] != NULL){
		 	if($_REQUEST['Kiln'] == true and $_REQUEST['Embedded'] == true){$DRval = "เตาเผาซาก-ที่ฝังซาก";}
		     elseif($_REQUEST['Kiln'] == true and $_REQUEST['Embedded'] == false){$DRval = "เตาเผาซาก";}
			 else { $DRval = "ที่ฝังซาก" ;} 
		 }
		 if($_REQUEST['Cleaning'] != NULL){$DayClean = $_REQUEST['DayClean'];}
		 $Food = $_REQUEST['Food'];
		 if($_REQUEST['FoodCompany_1'] != NULL){ $Food .= "-".$_REQUEST['FoodCompany_1']; }
		 elseif ($_REQUEST['FoodCompany_2'] != NULL){ $Food .= "-".$_REQUEST['FoodCompany_2'];}
		 /*------------ Animal ------------*/
		 $table_name = "";
		 $field_name = "";
			$value = "";
			$animal = $_REQUEST['Animal'];
			$type_pig = "";
			if($_REQUEST['PicOld'] != NULL){
				$type_pig .= $_REQUEST['PicOld']."-";
			}
			if($_REQUEST['PicYoung'] != NULL){
				$type_pig .= $_REQUEST['PicYoung']."-";
			}
			if($_REQUEST['PicSun'] != NULL){
				$type_pig .= $_REQUEST['PicSun']."-";
			}
			if($_REQUEST['PicFeed'] != NULL){
				$type_pig .= $_REQUEST['PicFeed'];
			}else{
				$type_pig = substr($type_pig,0,strlen($type_pig)-1);
			}
			if($animal == 1 and $NumAnimal == 1){
				$NumCow = $_REQUEST['NumCow'];
			}
			elseif ($animal == 2 and $NumAnimal == 2){
				$NumPic = $_REQUEST['NumPic'];
				$NumFather = $_REQUEST['NumFather'];
				$NumMother = $_REQUEST['NumMother'];
				$NumFatten = $_REQUEST['NumFatten'];
				$NumNursery = $_REQUEST['NumNursery'];
				$NumFeed = $_REQUEST['NumFeed'];
				$Abbattoir_1 = $_REQUEST['Abbattoir_1'];
				$Abbattoir_2 = $_REQUEST['Abbattoir_2'];
				$Abbattoir_3 = $_REQUEST['Abbattoir_3'];
				$NumAbb_1 = $_REQUEST['NumAbb_1'];
				$NumAbb_2 = $_REQUEST['NumAbb_2'];
				$NumAbb_3 = $_REQUEST['NumAbb_3'];
			}
			elseif($animal == 3 and $NumAnimal == 3){
				$FarmCompany = $_REQUEST['FarmCompany'];
				$ChickenFarm = $_REQUEST['NameAgency'];
				$CockNum = $_REQUEST['CockNum'];
				$CompanyCock = $_REQUEST['CompanyCock'];
			}
			elseif($animal == 4 and $NumAnimal == 4){
				$Chicken = "";
				if($_REQUEST['ChickenOldEgg'] != NULL){ $Chicken .= $_REQUEST['ChickenOldEgg']."-";}
				if($_REQUEST['ChickenYoungEgg'] != NULL){ $Chicken .= $_REQUEST['ChickenYoungEgg']."-";}
				if($_REQUEST['ChickenOldMeat'] != NULL){ $Chicken .= $_REQUEST['ChickenOldMeat']."-";}
				if($_REQUEST['ChickenYoungMeat'] != NULL){ $Chicken .= $_REQUEST['ChickenYoungMeat']; }
				else{$Chicken = substr($Chicken,0,strlen($Chicken)-1);}
				$Chicken_num = $_REQUEST['Chicken'];
			}
			elseif($animal == 5 and $NumAnimal == 5){
				$ChickenEgg = $_REQUEST['ChickenEgg'];
			}
			elseif($animal == 6 and $NumAnimal == 6){
				$duck_type = "";
				if($_REQUEST['DuckOldEgg'] != NULL){ $duck_type .= $_REQUEST['DuckOldEgg']."-"; }
				if($_REQUEST['DuckYoungEgg'] != NULL){ $duck_type .= $_REQUEST['DuckYoungEgg']."-"; }
				if($_REQUEST['DuckOldMeat'] != NULL){ $duck_type .= $_REQUEST['DuckOldMeat']; }
				else{$duck_type = substr($duck_type,0,strlen($duck_type)-1);}
				$Duck_num = $_REQUEST['Duck'];
			}
			
			elseif($animal == 7 and $NumAnimal == 7){
				$DuckCompany = $_REQUEST['DuckCompany'];
				$DuckFree = $_REQUEST['txtDuckFreedom'];
				$DuckMeat = $_REQUEST['DuckMeat'];
				$KillerDuck = $_REQUEST['CompanyDuck'];
			}
			elseif($animal == 8 and $NumAnimal == 8){
				$animalOther = $_REQUEST['txtOther'];
				$OtherAnimal = $_REQUEST['OtherAnimal'];
			}
			
			$Resource = $_REQUEST['Resource'];
			if($_REQUEST['CompanyName'] != NULL){ $Resource .= "-".$_REQUEST['CompanyName'];}
			if($_REQUEST['OtherName'] != NULL){ $Resource .= "-".$_REQUEST['OtherName'];}
			$NameSpecies = $_REQUEST['NameSpecies'];
					 
		 /*-------------- Veterinary -----------*/
		 $NameVeterinary = $_REQUEST['NameVeterinary'];
		 $NumVeterinary = $_REQUEST['NumVeterinary'];
		 $VillageVeterinary = $_REQUEST['VillageVeterinary'];
		 $district_3 = $_REQUEST['district_3'];
		 $TelVeterinary = $_REQUEST['TelVeterinary'];
		 $PhoneVeterinary = $_REQUEST['PhoneVeterinary'];
		 $FaxVeterinary = $_REQUEST['FaxVeterinary'];
		 $PermitVeterinary = $_REQUEST['PermitVeterinary'];
		 $NumPermit = $_REQUEST['NumPermit'];
		 $ExpPermit = $_REQUEST['ExpPermit'];
		 
		 /*-------------- AnimalHusbandman -----------*/
		 $NameAnimalHusbandman = $_REQUEST['NameAnimalHusbandman'];
		 $NumAnimalHusbandman = $_REQUEST['NumAnimalHusbandman'];
		 $VillageAnimalHusbandman = $_REQUEST['VillageAnimalHusbandman'];
		 $district_4 = $_REQUEST['district_4'];
		 $TelAnimalHusbandman = $_REQUEST['TelAnimalHusbandman'];
		 $PhoneAnimalHusbandman = $_REQUEST['PhoneAnimalHusbandman'];
		 $FaxAnimalHusbandman = $_REQUEST['FaxAnimalHusbandman'];
		 if($_REQUEST['do'] == "add"){
		
			/*----------------------------------- ADD CERTIFICATE -------------------------------*/						
			$query_status_1 = $db->insert("certificate","Certificate_NO,Date_mft,Date_exp,Pusher,Pusher_Date,Recipients",
														"'',1/5/2555,1/5/2555,'$txtNameOwner',1/5/2555,'$Recipient'");
			$id_1 = mysql_insert_id(); //Key certificate
			/*----------------------------------- ADD CERTIFICATE -------------------------------*/	
			
			
			
			/*----------------------------------- ADD DISEASE -----------------------------------*/
			$query_status_2 = $db->insert("disease","Phase_6_Month,Phase_1_Year,Phase_3_Year,Phase_5_Year",
													"'$Month6','$Year1','$Year3','$Year5'");
			$id_2 = mysql_insert_id(); //Key disease
			/*----------------------------------- ADD DISEASE -----------------------------------*/
			
			
			
			/*----------------------------------- ADD VETERINARY -------------------------------*/
			$query_status_3 = $db->insert("veterinary",
											"Name,Address,Moo,ID_District,Tel,Phone,Fax,Permit,No_Permit,Exp_Permit",
											"'$NameVeterinary','$NumVeterinary',$VillageVeterinary,$district_3,
											'$TelVeterinary','$PhoneVeterinary','$FaxVeterinary',
											$PermitVeterinary,'$NumPermit',$ExpPermit"); 
			$id_3 = mysql_insert_id(); //Key veterinary
			/*----------------------------------- ADD VETERINARY -------------------------------*/
			
			
			/*----------------------------------- ADD ANIMAL_HUSBANDMAN ------------------------*/
			$query_status_4 = $db->insert("animal_husbandman",
											"Name,Address,Moo,ID_District,Tel,Phone,Fax",
											"'$NameAnimalHusbandman','$NumAnimalHusbandman',$VillageAnimalHusbandman,
											$district_4,'$TelAnimalHusbandman','$PhoneAnimalHusbandman','$FaxAnimalHusbandman'");
			$id_4 = mysql_insert_id(); //Key animal_husbandman
			
			/*----------------------------------- ADD ANIMAL_HUSBANDMAN ------------------------*/
			
			
			/*----------------------------------- ADD HOUSE ------------------------------------*/
			
			$query_status_5 = $db->insert("house",
											"SystemHouse,Roof,Ground,Size,Num",
											"'$House','$Roof','$Ground','$sizeHouse',$txtNumHouse");
			$id_6 = mysql_insert_id();
			
			
			/*----------------------------------- ADD HOUSE ------------------------------------*/
			
			
			/*----------------------------------- ADD FARM -------------------------------------*/
 			$query_status_6 = $db->insert("Farm",
											"Farm_ID,Name,Address,Moo,Tel,Phone,Fax,
											Type,MainStreet, MaketAnimals,
											Abattoir,Communities,PublicWater,Other, Area_ha,
											Area_performance,Area_table,Fence,Office, Home,
											Ascent,Pack_Animals,Water,Food, Vaccine,Registration_Animal,
											Mark_Animal,Coordinates_X,Coordinates_Y, ISO_Farm,Stable_Size,
											Stable_Num,Stable_Disease_Size,Stable_Disease_Num, Cesspool,
											Drainge_Of_Waste,Cleaning,Debris_Removal,Treatment_Number, 
											Treatment_Width,Treatment_Height,Treatment_Depth,Water_Disinfectant,
											Sprayed_Disinfectant, Record_Person,Num_Worker,Accommodation,
											ID_Certificate,ID_District, ID_Disease,ID_Veterinary,ID_Animal_Husbandman,
											ID_Type_Farm,ID_House",
											"'$txtReg','$txtNameFarm','$txtNumFarm',$txtNumVillageFarm,'$txtTelFarm',
											'$txtPhoneFarm','$txtFaxFarm','$typefarm',$MainWay,
											$MarketAnimal,$Abbattoir,$Village,$PublicWater,$OtherFarm,
											$Area_ha,$Area_work,$Area_table,'$Fence',$txtNumOffice,
											$txtNumHome,'$Ascent',$PackAnimals,'$Water','$Food',
											'$Vaccine',$RecordAnimal,$MarkAnimal,'','',
											$FarmISO,$SizeStableImport,$NumStableImport,$SizeStableDisease,$NumStableDisease,
											$Cesspool,$DrainageWaste,$DayClean,'$DRval',$NumPothole,
											$WidthPothole,$HeightPothole,$DepthPothole,$WaterTreatment,$SpraySolution,
											$Record,$NumWorker,'$HomeWorker',$id_1,$district_2,
											$id_2,$id_3,$id_4,$txtTypeFarm,$id_6");
			/*----------------------------------- ADD FARM -------------------------------------*/
			
			
			
			/*----------------------------------- ADD OWNER -------------------------------------*/
			$query_status_7 = $db->insert("Owner",
									"Initial,Name,Address,Moo,ID_District,Tel,Phone,Fax,Training_Date,ID_Farm",
									"'$Initial','$txtNameOwner','$NumHome',
									$txtNumVillage,'$Name','$Tel',
									'$Phone','$Fax',$datepicker,'$txtReg'"); 
			/*----------------------------------- ADD OWNER -------------------------------------*/	
			if($query_status_1 == 1){
				echo "บันทึกข้อมูลเรียบร้อยแล้ว <br/>";
				echo $id_1;
				//echo "<meta http-equiv='refresh' content='5;url=Information.html'>";	
			}else{
				 echo "ไม่สามารถบันทึกข้อมูลได้ !!! <br/>";
				 echo $query_status_1;
			}
			if($query_status_2 == 1){
				echo "บันทึกข้อมูลเรียบร้อยแล้ว <br/>";
				echo $id_2;
				//echo "<meta http-equiv='refresh' content='5;url=Information.html'>";	
			}else{
				 echo "ไม่สามารถบันทึกข้อมูลได้ !!! <br/>";
				 echo $query_status_2;
			}
			if($query_status_3 == 1){
				echo "บันทึกข้อมูลเรียบร้อยแล้ว <br/>";
				echo $id_3;
				//echo "<meta http-equiv='refresh' content='5;url=Information.html'>";	
			}else{
				 echo "ไม่สามารถบันทึกข้อมูลได้ !!! <br/>";
				 echo $query_status_3;
			}
			if($query_status_4 == 1){
				echo "บันทึกข้อมูลเรียบร้อยแล้ว <br/>";
				echo $id_4;
				//echo "<meta http-equiv='refresh' content='5;url=Information.html'>";	
			}else{
				 echo "ไม่สามารถบันทึกข้อมูลได้ !!! <br/>";
				 echo $query_status_4;
			}
			if($query_status_5 == 1){
				echo "บันทึกข้อมูลเรียบร้อยแล้ว <br/>";
				echo $id_5;
				//echo "<meta http-equiv='refresh' content='5;url=Information.html'>";	
			}else{
				 echo "ไม่สามารถบันทึกข้อมูลได้ !!! <br/>";
				 echo $query_status_5;
			}
			if($query_status_6 == 1){
				echo "บันทึกข้อมูลเรียบร้อยแล้ว <br/>";
				echo $id_6;
				//echo "<meta http-equiv='refresh' content='5;url=Information.html'>";	
			}else{
				 echo "ไม่สามารถบันทึกข้อมูลได้ !!! <br/>";
				 echo $query_status_6;
			}
			if($query_status_7 == 1){
				echo "บันทึกข้อมูลเรียบร้อยแล้ว <br/>";
				echo $id_7;
				//echo "<meta http-equiv='refresh' content='5;url=Information.html'>";	
			}else{
				 echo "ไม่สามารถบันทึกข้อมูลได้ !!! <br/>";
				 echo $query_status_7;
			}
			/*-------------------------------------------------------Animal-------------------------------------------------------*/
			$query_status_resource = $db->insert("resource","Name","'$Resource'");
			$id_8 = mysql_insert_id();
			$query_status_species = $db->insert("species","Name","'$NameSpecies'");
			$id_9 = mysql_insert_id();
			
			
			if($animal == 1){
				$table_name = "milk_cow";
				$field_name = "Num_Cow,ID_Farm,ID_Resource,ID_Species";
				$value = $NumCow.",'$txtReg',".$id_8.",".$id_9;
			}elseif($animal == 2){
				$table_name = "pig";
				$field_name = "Type,Num_All,Num_Breeder,Num_Breeding,Num_Fattening,Num_Nursery,Capacity,
								Abattoir_1,Abattoir_2,Abattoir_3,Num_Abattoir_1,Num_Abattoir_2,Num_Abattoir_3,
								ID_Farm,ID_Resource,ID_Species";
				$value = "'$type_pig',".$NumPic.",".$NumFather.",".$NumMother.",".$NumFatten.",".$NumNursery.",".$NumFeed.",
							'$Abbattoir_1','$Abbattoir_2','$Abbattoir_3',".$NumAbb_1.",".$NumAbb_2.",".$NumAbb_3.",'$txtReg',".$id_8.",".$id_9;
			}elseif($animal == 3 or $animal == 4 or $animal == 5){
				$table_name = "chicken";
				$field_name = "Name,Type,Agent_name,Num_Chicken,Killer_Company,ID_Farm,ID_Resource,ID_Species";
					if($animal == 3){
						$value = "'ไก่เนื้อ','$FarmCompany','$ChickenFarm',".$CockNum.",'$CompanyCock','$txtReg',".$id_8.",".$id_9;
					}
					elseif($animal == 4){
						$value = "'ไก่พันธุ์','$Chicken','',".$Chicken_num.",'','$txtReg',".$id_8.",".$id_9;
					}
					else { $value = "'ไก่ไข่','','',".$ChickenEgg.",'','$txtReg',".$id_8.",".$id_9;}
			}elseif($animal == 6 or $animal == 7){
				$table_name = "duck";
				$field_name = "Name,Type,Agent_name,Num_Duck,Killer_Company,ID_Farm,ID_Resource,ID_Species";
				if($animal == 6){
					$value = "'เป็ดพันธุ์','$duck_type','',".$Duck_num.",'','$txtReg',".$id_8.",".$id_9;
				}else{
					$value = "'เป็ดเนื้อ','$DuckCompany','$DuckFree',".$DuckMeat.",'$KillerDuck','$txtReg',".$id_8.",".$id_9;
				}
			}else{
				$table_name = "animal_other";
				$field_name = "Name,Num_Other,ID_Farm,ID_Resource,ID_Species";
				$value = "'$animalOther',".$OtherAnimal.",'$txtReg',".$id_8.",".$id_9;
			}
			$query_status_animal = $db->insert("$table_name","$field_name","$value");
				if($query_status_animal == 1){
					echo "บันทึกเรียบร้อย";
			}else{
				echo $query_status_animal;
			}
		}//end add
}else{
	echo " ไม่สามารถติดต่อฐานข้อมูลได้ ";
}
?>